package com.ymatou.iapi.m.couponservice.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.CouponInfo;
import com.ymatou.iapi.m.couponservice.parameter.GetCouponInfoesByCodeBean;
import com.ymatou.iapi.m.couponservice.service.GetCouponInfoesByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.TestDataManager;
/**
*根据优惠券编号获取优惠券信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCouponInfoesByCode{
	private static GetCouponInfoesByCodeBean getcouponinfoesbycodeBean;
	private static GetCouponInfoesByCodeCall getcouponinfoesbycodeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据优惠券编号获取优惠券信息");
	}

	@Before
	public void caseUp() {
		getcouponinfoesbycodeBean=new GetCouponInfoesByCodeBean();
		getcouponinfoesbycodeCall=new GetCouponInfoesByCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcouponinfoesbycodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponInfoesByCode_001() {
		Logger.start(true,"根据优惠券编号获取优惠券信息");
		try {
			List<YmtCoupon> coupons = new YmtCouponWapper().selectTop5();
			
			List<String> couponCodes = new ArrayList<>();
			
			for (YmtCoupon coupon : coupons) {
				couponCodes.add(coupon.getsCouponCode());
			}
			getcouponinfoesbycodeBean.setCouponCodes(couponCodes);
			
			getcouponinfoesbycodeCall.setData(getcouponinfoesbycodeBean);
			getcouponinfoesbycodeCall.callService();	
			
			Logger.verifyEquals("0", getcouponinfoesbycodeCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponinfoesbycodeCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponinfoesbycodeCall.getCouponInfos().getCouponInfoes();
			List<String> returnCouponCodes = new ArrayList<>();
			
			for (int i=0;i<couponInfos.size();i++) {
				CouponInfo couponInfo = couponInfos.get(i);

				returnCouponCodes.add(couponInfo.getCouponCode());
				
				YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(couponInfo.getCouponCode()).get(0);
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(coupon.getiBatchId());
				YmtCouponValue value = new YmtCouponValueWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				YmtCouponPrivateUserBound userBound = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(couponInfo.getCouponCode()).get(0);
				
				Logger.verifyEquals(batch.getiBatchId(), couponInfo.getBatchId(), "验证返回batchId");
				Logger.verifyEquals(batch.getsBatchCode(), couponInfo.getBatchCode(), "验证返回batchCode");
				Logger.verifyEquals(0, couponInfo.getOrderMinAmount().compareTo(value.getFminordervalue()), "验证返回orderMinAmount");
				Logger.verifyEquals(0, couponInfo.getCouponAmount().compareTo(value.getFcouponvalue()), "验证返回couponAmount");
				
				// JIRACONN-11615
				Logger.verifyEquals(userBound.getiCouponUsedCount(), couponInfo.getRemainUseTimes(), "验证返回remainUsedTimes");

			}
			
			Logger.verifyEquals(true,TestDataManager.isListEqual(returnCouponCodes, couponCodes), "验证返回CouponCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponInfoesByCode_002() {
		Logger.start(true,"根据优惠券编号获取优惠券信息couponcodes空");
		try {
			
			getcouponinfoesbycodeCall.setData(getcouponinfoesbycodeBean);
			getcouponinfoesbycodeCall.callService();	
			
			Logger.verifyEquals("0", getcouponinfoesbycodeCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponinfoesbycodeCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponinfoesbycodeCall.getCouponInfos().getCouponInfoes();
			
			Logger.verifyEquals(0, couponInfos.size(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}